package com.drinks.yiniang.adapter.photo;

import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.base.BaseQuickRCVAdapter;
import com.drinks.yiniang.common.camera.album.Result;
import com.drinks.yiniang.entity.photo.Photos;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.ui.widget.image.RatioRoundImageView;

import java.util.List;

/**
 * 草稿adapter
 * @author han
 * @date 2019/8/30
 */
public class DraftsPhotos2Adapter extends BaseQuickRCVAdapter<Photos, BaseViewHolder> {

    private AlbumPhotosAdapter.OnAlbumPhotoChangeListener onAlbumPhotoChangeListener;

    public DraftsPhotos2Adapter(Context mContext, @Nullable List<Photos> data) {
        super(R.layout.item_album_photo_drafts, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, Photos item) {
        GlideUtils.load(mContext, (RatioRoundImageView) helper.getView(R.id.iv_album_photos_drafts), item.path);

        updateSelector((TextView) helper.getView(R.id.tv_album_photos_drafts_choose), Result.isSelected(item), item);

        setChooseClick(helper.getView(R.id.v_album_photos_drafts_choose), item);
    }

    private void updateSelector(TextView tvSelector, boolean selected, Photos photo) {
        if (selected) {
            String number = Result.getSelectorNumber(photo);
            if ("0".equals(number)) {
                tvSelector.setBackgroundResource(R.mipmap.icon_photo_choose_n);
                tvSelector.setText(null);
                return;
            } else {
                tvSelector.setText(number);
                tvSelector.setBackgroundResource(R.drawable.shape_bkg_photo_choose_num);
            }
        } else {
            tvSelector.setBackgroundResource(R.mipmap.icon_photo_choose_n);
            tvSelector.setText(null);
        }
    }

    private void setChooseClick(View v, final Photos photo) {
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isSelected = Result.isSelected(photo);

                if (isSelected) {
                    Result.removePhotos(photo);
                } else {
                    Result.addPhotos(photo);
                }

                if(onAlbumPhotoChangeListener != null) {
                    onAlbumPhotoChangeListener.onPhotosChange(DraftsPhotos2Adapter.this);
                }
            }
        });
    }

    public void setOnAlbumPhotoChangeListener(AlbumPhotosAdapter.OnAlbumPhotoChangeListener onAlbumPhotoChangeListener) {
        this.onAlbumPhotoChangeListener = onAlbumPhotoChangeListener;
    }
}
